package com.pmh.hrs.rhr.controller.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Controller
public class LoginInterceptor extends HandlerInterceptorAdapter {

	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
		HttpSession session = request.getSession(false);
		String user_id = "";
		if(session!=null){
			user_id = (String) session.getAttribute("user_id");
		}
		user_id = (user_id==null) ? "" :user_id;
		
		System.out.println("AAAAAAAAAAAAAAAAAAAA");
		//Session이 존재하지 않거나 session값안의 userId가 존재하지 않는경
	//	if(session ==null || "".equals(user_id)){
		//	response.sendRedirect("/");
			//return false;
		//}
		// 보안처리 extraParam 변조 방지
		//request.setAttribute("user_auth", session.getAttribute("user_auth"));
		
		return true;
	}
		private boolean isConvertTargetJsonP(Object handler) throws Exception{
			String targetClazzName ="org.springframework.web.method.HandlerMethod";
			String sourceClazzName =handler.getClass().getName();
			
			if(targetClazzName.equals(sourceClazzName)){
				HandlerMethod handlerMethod = (HandlerMethod) handler;
				String methodName =handlerMethod.getMethod().getName();
				
				if(methodName.contains("select")){
					return true;
				}
				
			}
		
			return false;
		}
	 @Override
	    public void afterCompletion(HttpServletRequest request,
	            HttpServletResponse response, Object handler, Exception ex)
	            throws Exception {
	        String a ="AAA";

	        if(this.isConvertTargetJsonP(handler)){
	        	//JSONP로 변환한다..
	        	System.out.println("#############");
	        	
	        }
	        //Exception처리 부분... 
	        
	        
	        //JSONP로 할지 JSON으로 리턴할치 처리부
	        
	     
	        
	        
	    }
	@Override
	public void postHandle(
			HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
	 
		System.out.println("###########");
		long endTime = System.currentTimeMillis();
			//modified the exisitng modelAndView
			
	 
		}
}
